package com.congee02;

import java.util.*;

public class RightSideView_Level {

    public List<Integer> rightSideView(TreeNode root) {
        if (root == null) {
            return List.of();
        }

        List<Integer> ans = new ArrayList<>();

        ArrayDeque<TreeNode> q = new ArrayDeque<>();

        q.offer(root);

        while (! q.isEmpty()) {
            int n = q.size();
            ans.add(q.peekLast().val);
            for (int i = 0 ; i < n ; i ++ ) {
                TreeNode cur = q.poll();
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
            }
        }

        return ans;
    }

}
